package com.cncbox.controller;

import com.cncbox.common.Utils;
import com.cncbox.dto.EnhanceArticle;
import com.cncbox.entity.Article;
import com.cncbox.entity.NoteResult;
import com.cncbox.service.ArticleService;
import com.sun.istack.internal.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author Develop_Peng
 * @version V1.0
 * @Package com.cncbox.controller
 * @Description: 文章类
 * @date 2016/3/31 11:19
 */

@Controller
@RequestMapping(value = "/manager")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 添加文章
     *
     * @param resourceFile       资源标识
     * @param httpServletRequest 根据Request来获取项目路径
     * @param enhanceArticle     传入相关参数
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/increaseArticle")
    public String increaseArticle(@RequestParam MultipartFile resourceFile, HttpServletRequest httpServletRequest, EnhanceArticle enhanceArticle) throws Exception {
        NoteResult result = new NoteResult();
        //进行参数判断
        if (enhanceArticle != null && resourceFile.getSize() > 0) {
            //获取文件后缀名
            String suffix = resourceFile.getOriginalFilename().substring(resourceFile.getOriginalFilename().lastIndexOf("."));

            //处理上传文件名（时间）+后缀
            String fileName = Utils.notSymbol(new Date()) + suffix;

            //设置上传文件路径
            enhanceArticle.setFilePath(fileName);

            //添加上传时间
            enhanceArticle.setAddTime(Utils.format(new Date()));

            //进行参数返回
            result = articleService.addArticle(enhanceArticle);

            //判断返回参数，并进行存储数据
            if (result.getStatus() == 1) {
                String imagePath = httpServletRequest.getSession().getServletContext().getRealPath("resource/image");
                //进行写入文件
                Utils.inputFile(resourceFile, imagePath, fileName);
            } else {
                result.setStatus(0);
                result.setMsg("ADD_ERROR");
            }
        } else {
            result.setStatus(0);
            result.setMsg("PARAMETER_ERROR");
        }
        httpServletRequest.setAttribute("message", result);
        return "redirect:/manager/jump.html";
    }

    /**
     * 获取文章
     *
     * @param article 传入查询模块名
     * @return 返回所查询到数据
     * @throws Exception
     */
    @RequestMapping(value = "/obtainArticle")
    @ResponseBody
    public NoteResult obtainArticle(Article article) throws Exception {
        NoteResult noteResult = new NoteResult();
        if (article != null) {
            noteResult = articleService.selectOrderByClass(article);
        } else {
            noteResult.setStatus(0);
            noteResult.setMsg("PARAMETER_ERROR");
        }
        return noteResult;
    }

    /**
     * 修改前的数据查询
     *
     * @param quId 根据传入查询数据
     * @return 返回相关数据
     * @throws Exception
     */
    @RequestMapping(value = "/queryArticle")
    @ResponseBody
    public NoteResult queryArticle(Integer quId) throws Exception {
        NoteResult noteResult = new NoteResult();
        if (quId != null) {
            noteResult = articleService.queryArticleById(quId);

        } else {
            noteResult.setStatus(0);
            noteResult.setMsg("PARAMETER_ERROR");
        }
        return noteResult;
    }


    /**
     * 修改文章
     *
     * @param resourceFile   资源标识
     * @param enhanceArticle 相关参数
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/renewalArticle")
    public String renewalArticle(@RequestParam MultipartFile resourceFile, HttpServletRequest httpServletRequest, EnhanceArticle enhanceArticle) throws Exception {
        NoteResult noteResult = new NoteResult();
        //查询Id是否存在
        Integer resourceId = enhanceArticle.getResourceId();
        if (0 < resourceId) {
            //进行资源的判断
            if (resourceFile != null && resourceFile.getSize() > 0) {
                //获取文件后缀名
                String suffix = resourceFile.getOriginalFilename().substring(resourceFile.getOriginalFilename().lastIndexOf("."));

                //处理上传文件名（时间）+后缀
                String fileName = Utils.notSymbol(new Date()) + suffix;

                //设置上传文件路径
                enhanceArticle.setFilePath(fileName);
                String imagePath = httpServletRequest.getSession().getServletContext().getRealPath("resource/image");
                //进行写入文件
                Utils.inputFile(resourceFile, imagePath, fileName);
            }
            noteResult = articleService.updateByResourceId(enhanceArticle);
        } else {
            noteResult.setStatus(0);
            noteResult.setMsg("PARAMETER_ERROR");
        }
        httpServletRequest.setAttribute("message", noteResult);
        return "redirect:/manager/jump.html";
    }


    /**
     * 删除数据
     *
     * @param id 根据传入Id
     * @return 返回结果
     * @throws Exception
     */
    @RequestMapping(value = "/removeArticle")
    public String removeArticle(HttpServletRequest httpServletRequest, Integer id) throws Exception {
        NoteResult noteResult = new NoteResult();
        if (0 < id) {
            noteResult = articleService.removeArticleById(id);
        } else {
            noteResult.setStatus(0);
            noteResult.setMsg("PARAMETER_ERROR");
        }
        httpServletRequest.setAttribute("message", noteResult);
        return "redirect:/manager/jump.html";
    }


}
